package com.zls.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zls.mapper.TDeviceMapper;
import com.zls.page.PageResult;
import com.zls.page.QueryPageBean;
import com.zls.pojo.SysUser;
import com.zls.pojo.TDevice;
import com.zls.pojo.TFarm;
import com.zls.service.TDeviceService;
import com.zls.service.TFarmService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class TDeviceServiceImpl extends ServiceImpl<TDeviceMapper, TDevice> implements TDeviceService {
    @Autowired
    private TDeviceMapper tDeviceMapper;

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        PageResult pageResult = new PageResult();
        Integer currentPage = queryPageBean.getCurrentPage();//当前页
        Integer pageSize = queryPageBean.getPageSize();//每页大小
        String queryString = queryPageBean.getQueryString();//查询条件
        //total总记录数
        List<TDevice> page = tDeviceMapper.findPage((currentPage - 1) * pageSize, pageSize,queryString);
        //rows  当前页的数据
        Long count = tDeviceMapper.count(queryString);
        pageResult.setRows(page);
        pageResult.setTotal(count);
        return pageResult;
    }


    @Autowired
    private TFarmService tFarmService;
    @Override
    @Transactional
    public int addTDevice(TDevice tDevice) {
        tDevice.setCreateTime(new Date());
        tDevice.setInstallTime(new Date());
        tDevice.setUpdateTime(new Date());
        TFarm tFarm = tFarmService.selectId(tDevice.getFarmId());
        tDevice.setUserId(tFarm.getUserId());
        int insert = tDeviceMapper.insert(tDevice);
        return insert;
    }

    @Override
    public TDevice selectById(int id) {
        return tDeviceMapper.findById(id);
    }

    @Override
    @Transactional
    public int editTDevice(TDevice tDevice) {
        tDevice.setUpdateTime(new Date());
        TFarm tFarm = tFarmService.selectId(tDevice.getFarmId());
        tDevice.setUserId(tFarm.getUserId());
        int i = tDeviceMapper.updateById(tDevice);
        return i;
    }

    @Override
    public List<Integer> findIds() {
        return tDeviceMapper.findIds();
    }

    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Override
    public void findAll() {
        List<TDevice> tDevices = tDeviceMapper.selectList(null);
        List<Map<String,String>> list=new ArrayList<>();
        List<Integer> ids=new ArrayList<>();
        tDevices.stream().forEach(o->{
            Map<String,String> map=new HashMap<>();
            ids.add(o.getId());
            map.put("min",o.getThresholdMin()+"");
            map.put("max",o.getThresholdMax()+"");
            list.add(map);
        });
        for (int i = 0; i <ids.size() ; i++) {
            stringRedisTemplate.opsForHash().putAll(String.valueOf(ids.get(i)),list.get(i));
        }
    }
}
